package com.wangsd.web.api.controller;

import com.wangsd.common.utils.HttpClientUtils;
import com.wangsd.common.utils.MD5Utils;
import com.wangsd.web.api.service.TestRedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.SortedMap;
import java.util.TreeMap;

@Api(tags = "接口管理")
@RestController
@RequestMapping("/api/open")
@Slf4j
public class TestController {

    @Value("${notify_url}")
    private String notify_url;
    @Autowired
    TestRedisService testRedisService;

    /**
     * test
     * 测试回调
     */
    @ApiOperation(value = "测试订单")
    @PostMapping("/testCreate")
    public String testCreate(@ApiParam(value = "支付标识", required = true) @RequestParam String payCode,
                             @ApiParam(value = "金额", required = true) @RequestParam Double amount,
                             @ApiParam(value = "客户编号", required = true) @RequestParam String customerNo,
                             @ApiParam(value = "api秘钥", required = true) @RequestParam String apiKey) {
        SortedMap<String, String> param = new TreeMap();
        param.put("customerNo", customerNo);
        param.put("outTradeNo", System.currentTimeMillis() + "");
        param.put("notifyUrl", notify_url + "/api/notify/testNotify");
        param.put("payCode", payCode);
        Double amount2 = amount * 100;
        param.put("amount", amount2.intValue() + "");
        param.put("sign", MD5Utils.getMD5Sign(param, apiKey, 1));

        String str = HttpClientUtils.post(notify_url + "/api/open/createOrder", param, "UTF-8");
        return str;
    }
}
